package com.example.lock;

import com.example.util.ZKUtil;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestLock {
    private ZooKeeper zk;

    @Before
    public void before() {
        zk = ZKUtil.getZKClient();
    }

    @After
    public void after() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void lock() {



        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                String threadName = Thread.currentThread().getName();

                WatchCallback watchCallback = new WatchCallback(threadName, zk);
                watchCallback.tryLock();

//                try {
//                    Thread.sleep(1111);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(threadName + "   on working......");

                watchCallback.unLock();


            }).start();
        }


        while (true) {
            try {
                Thread.sleep(1111);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}
